package com.example.task.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.sound.midi.MidiDevice.Info;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.example.task.entity.TaskInfo;
import com.example.task.service.TaskService;

@Controller
@RequestMapping(value="/task")
public class TaskManageController {

	@Autowired(required=false)
	private TaskService taskService;

	/**
	 * Index.jsp
	 */
	@RequestMapping(value={"", "/", "index"})
	public String info(){
		
		return "index";
	}
	
	/**
	 * 任务列表
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/tasklist")
	public Map list(){
		Map<String, Object> map = new HashMap<>();
		List<TaskInfo> infos = taskService.list();
		map.put("rows", infos);
		map.put("total", infos.size());
		return map;
	}
	
	/**
	 * 保存定时任务
	 * @param info
	 */
	@ResponseBody
	@RequestMapping(value="/save", produces = "application/json; charset=UTF-8")
	public Map save(TaskInfo info){
		Map<String, Object> result=new HashMap<>();
		try {
			
			if(info.getJobId()==null||"".equals(info.getJobId())) {
				info=new TaskInfo();
				info.setExeclazz("com.myjob.test.TestJob1");
				info.setTaskName("MyJob3");
				info.setGroup("test333");
				info.setDesc("第三个测试任务");
				info.setJobId("100011");
				info.setCreateTime("2018-09-01 00:00:00");
				info.setCronExpression("0 */10 * * *  ?");
				taskService.addJob(info);
							}else{
				taskService.edit(info);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		result.put("code", 200);
		
		return result;
	}
	
	/**
	 * 删除定时任务
	 * @param jobName
	 * @param jobGroup
	 */
	@ResponseBody
	@RequestMapping(value="delete/{jobName}/{jobGroup}", produces = "application/json; charset=UTF-8")
	public String delete(@PathVariable String jobName, @PathVariable String jobGroup){
		try {
			taskService.delete(jobName, jobGroup);
		} catch (Exception e) {
			return e.getMessage();
		}
		return "成功";
	}
	
	/**
	 * 暂停定时任务
	 * @param jobName
	 * @param jobGroup
	 */
	@ResponseBody
	@RequestMapping(value="pause/{jobName}/{jobGroup}", produces = "application/json; charset=UTF-8")
	public String pause(@PathVariable String jobName, @PathVariable String jobGroup){
		try {
			taskService.pause(jobName, jobGroup);
		} catch (Exception e) {
			return e.getMessage();
		}
		return "成功";
	}
	
	/**
	 * 重新开始定时任务
	 * @param jobName
	 * @param jobGroup
	 */
	@ResponseBody
	@RequestMapping(value="resume/{jobName}/{jobGroup}", produces = "application/json; charset=UTF-8")
	public String resume(@PathVariable String jobName, @PathVariable String jobGroup){
		try {
			taskService.resume(jobName, jobGroup);
		} catch (Exception e) {
			return e.getMessage();
		}
		return "成功";
	}

}
